package com.abl.service.system.impl;

import com.abl.common.mybatis.dao.Dao;
import com.abl.common.mybatis.model.Dnt;
import com.abl.common.mybatis.strengthen.Wrapper;
import com.abl.domain.model.AblError;
import com.abl.domain.model.Param;
import com.abl.common.util.StringKit;
import com.abl.common.util.coll.ArrayKit;
import com.abl.service.system.SysAPIService;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源表实现
 */
@Slf4j
@Service
public class SysAPIServiceImpl implements SysAPIService {
    @Autowired
    private Dao dao;

    //新增
    @Override
    public long add(Param<JSONObject> param) {
        //校验path重复
        String path = param.getString("path");
        String api_name = param.getString("api_name");
        String module_name = param.getString("module_name");
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("path", path);
        qw.eq("api_name", api_name);
        qw.eq("module_name", module_name);
        long sys_api = dao.count("sys_api", qw);
        if (sys_api > 0) throw new AblError("操作失败：【模块:{0}】【API:{1}】【请求地址:{2}】已经存在", module_name, api_name, path);

        Dnt dnt = Dnt.createInsert(param);

        return dao.insert("sys_api", dnt);
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean del(Param<JSONObject> param) {
        ArrayList<Object> ids = param.getList("ids");
        Wrapper<Object> uw = new Wrapper<>();
        uw.in("id", ids);
        uw.set("d_s", "1");
        dao.update("sys_api", uw);
        return true;
    }

    //修改
    @Override
    public long edit(Param<JSONObject> param) {
        //校验path重复
        String id = param.getString("id");
        String path = param.getString("path");
        String api_name = param.getString("api_name");
        String module_name = param.getString("module_name");
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq(param, "path", "api_name", "module_name");
        qw.ne("id", id);

        long sys_api = dao.count("sys_api", qw);
        if (sys_api > 0) throw new AblError("操作失败：【模块:{0}】【API:{1}】【请求地址:{2}】已经存在", module_name, api_name, path);

        Dnt dnt = Dnt.createUpdate(param);

        return dao.updateById("sys_api", dnt);
    }

    //查询详情
    @Override
    public Dnt info(Param<JSONObject> param) {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.eq("id", param.getOrDefault("id", 0));
        return dao.selectOne("sys_api", wrapper);
    }

    //查询详情
    @Override
    // @Cacheable(value = "ref_info", key = "#path")
    public Dnt info(String path) {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.eq("d_s", 0);
        wrapper.eq("path", path);
        return dao.selectOne("sys_api", wrapper);
    }

    //查询集合
    @Override
    public List<Dnt> list(Param<JSONObject> param) {
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eqNotBlank(param, "id", "request_type", "check_login", "check_permission", "log_open_action");
        qw.likeNotBlank(param, "path", "api_name", "use_state");
        qw.orderByAsc("sort");
        qw.orderByAsc("path");
        if (param.isPage()) {
            return dao.page("sys_api", qw, param.pageCurrent(), param.pageSize());
        } else {
            return dao.list("sys_api", qw);
        }
    }

    //模糊查询条件下拉列表
    @Override
    public List<Dnt> searchInput(Param<JSONObject> param) {
        String sys_name = param.getString("name", "");
        String sys_path = param.getString("path", "");

        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.last(" limit 30 ");

        if (StringKit.isNotBlank(sys_name)) {
            qw.select("name");
            qw.like("name", sys_name);
            qw.groupBy("name");
            return dao.list("sys_api", qw);
        }

        if (StringKit.isNotBlank(sys_path)) {
            qw.select("path");
            qw.like("path", sys_path);
            qw.groupBy("path");
            return dao.list("sys_api", qw);
        }

        return new ArrayList<>();
    }

    @Override
    public List<Dnt> moduleNameList(Param<JSONObject> param) {
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq(StringKit.isNotBlank(param.getString("id")), "id", param.getString("id"));
        qw.select("module_name,count(module_name) count");
        qw.groupBy("module_name");
        List<Dnt> sys_api = dao.list("sys_api", qw);

        if (ArrayKit.isNotBlank(sys_api)) {
            return sys_api.stream().sorted(Comparator.comparing(e -> e.getString("module_name"))).collect(Collectors.toList());
        }

        return sys_api;
    }


}
